# -*-* encoding:UTF-8 -*-
# author            : mengy
# date              : 2018/12/18
# python-version    : Python 3.7.0
# description       : 爬取百度图片

import requests
import json
import time


def request_url(keyword, page):
    "百度搜索相关图片"
    # url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=美女&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=0&word={}&s=&se=&tab=&width=&height=&face=0&istype=2&qc=&nc=1&fr=&cg=girl&pn={}&rn=30&gsm=1e'.format(
    #     keyword, page
    # )
    url = 'https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={}&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=&copyright=&word={}&s=&se=&tab=&width=&height=&face=&istype=&qc=&nc=1&fr=&expermode=&pn={}&rn=30&gsm=96&1545124198863='.format(
        keyword, keyword, page * 30
    )

    response = requests.get(url)
    if response.status_code == 200:
        return response.text


def get_url(content):
    "获取图片下载地址"
    # print(content)
    datas = json.loads(content, strict=False)['data']
    urlset = set()
    for data in datas:
        try:
            urlset.add(data['thumbURL'])
        except Exception as e:
            pass
    return urlset


def save_images(url):
    "保存图片到本地"
    response = requests.get(url)
    # name = url.split(',')[1].split('&')[0]
    img_type = url.split('.')[-1]
    if response.status_code == 200:
        with open(r'resources/images/' + str(time.time()) + '.' + img_type, 'wb') as f:
            print('正在从%s下载图片' % url)
            f.write(response.content)
        return True
    else:
        return False


if __name__ == '__main__':
    keyword = input("请输入需要搜索图片的关键字:")
    page = input("请输入需要下载的页数:")
    total = 0
    start = time.time()
    for i in range(int(page)):
        print("正在搜索关于{}的第{}页图片".format(keyword, (i + 1)))
        content = request_url(keyword, i)
        urlset = get_url(content)
        for url in urlset:
            if save_images(url):
                total += 1
    end = time.time()
    print('下载完成，成功下载{}张图片,总耗时:{}s' .format(total,(end - start)))

    # url = 'https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=1875481582,4245977543&fm=26&gp=0.jpg'
    # print(url.split('.')[-1])
